class IUP_SUBMENU
-- Creates a menu item that, when selected, opens another menu.
-- In Motif and GTK, the text font will be affected by the dialog font when the 
-- menu is mapped.

inherit
	IUP_MENU_ELEMENT
		redefine
			execute_map,
			execute_unmap,
			execute_destroy,
			execute_highlight
		end
	IUP_WIDGET_TITLE
	IUP_WIDGET_ACTIVE
	IUP_WIDGET_NAME

create {ANY}
	submenu

feature {ANY}

	submenu (title: STRING; menu: IUP_MENU)
		-- A new submenu.
		-- title: String containing the text to be shown on the item. It can be 
		-- Void.
		-- menu: child menu identifier.
		local
			a_submenu, a: POINTER
		do
			if title /= Void then
				a := get_pointer(title.to_c)
			end
			
			a_submenu := int_submenu(a, menu.widget)
			set_widget(a_submenu)
		end

	-- Attributes

	set_image (name: STRING)
		-- [Windows and GTK Only] (non inheritable): Image name of the submenu 
		-- image. In Windows, an item in a menu bar cannot have a check mark. 
		-- Ignored if submenu in a menu bar. A recommended size would be 16x16 to 
		-- fit the image in the menu item. In Windows, if larger than the check 
		-- mark area it will be cropped.
		do
			iup_open.set_attribute(Current, "IMAGE", name)
		end

	-- Callbacks

	-- Common
	set_cb_map (act: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING])
		-- Called right after an element is mapped and its attributes updated.
		local
			operation: INTEGER
		do
			cb_map := act
			
			if cb_map /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
		end

	set_cb_unmap (act: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING])
		-- Called right before an element is unmapped.
		local
			operation: INTEGER
		do
			cb_unmap := act

			if cb_unmap /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
		end

	set_cb_destroy (act: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING])
		-- Called right before an element is destroyed.
		local
			operation: INTEGER
		do
			cb_destroy := act

			if cb_destroy /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
		end

	-- Extra
	set_cb_highlight (act: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING])
		-- Action generated when the item is highlighted.
		local
			operation: INTEGER
		do
			cb_highlight := act

			if cb_highlight /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "HIGHLIGHT_CB", "NONEEDED", operation)
		end

feature {ANY}

	-- Common callbacks
	
	execute_map: STRING
		do
			if attached cb_map as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_unmap: STRING
		do
			if attached cb_unmap as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_destroy: STRING
		do
			if attached cb_destroy as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	-- Extra

	execute_highlight: STRING
		do
			if attached cb_highlight as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end


feature {NONE}

	-- For callbacks
	
	cb_map: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING]
	cb_unmap: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING]
	cb_destroy: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING]

	cb_highlight: detachable FUNCTION[TUPLE[IUP_SUBMENU], STRING]

	-- Internals
	
	int_submenu(title, menu: POINTER): POINTER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupSubmenu ($title, $menu);"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
